/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-4-30</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.xianl.busiplat.common.SecurityConstants;
import com.xianl.busiplat.dao.BaseDao;
import com.xianl.busiplat.dao.ModuleDao;
import com.xianl.busiplat.po.Module;
import com.xianl.busiplat.service.ModuleService;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-4-30
 */
public class ModuleServiceImpl extends BaseServiceImpl<Module> implements ModuleService{

	private ModuleDao mapper;
	
	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.ModuleService#getMTree(java.util.Set)
	 */
	@Override
	public Module getTree(Set<String> permissionSet) {
		List<Module> list = null;
		try {
			list  = mapper.findTree();
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Module> rootList = makeTree(list,permissionSet);
		return rootList.get(0);
	}

	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.ModuleService#getTree()
	 */
	@Override
	public Module getTree() {
		List<Module> list = null;
		try {
			list  = mapper.findTree();
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Module> rootList = makeTree(list,null);
		return rootList.get(0);
	}
	
	/**
	 * desc:
	 * <p>author: Jan.Zhang </p>
	 *  date : 2014-5-4 
	 * @param list
	 * @param permissionSet
	 * @return
	 */
	private List<Module> makeTree(List<Module> list, Set<String> permissionSet) {
		List<Module> parent = new ArrayList<Module>();
		for (Module e : list) {
			if (e.getParent() == null) {
				e.setChildren(new ArrayList<Module>(0));
				parent.add(e);
			}
		}
		list.removeAll(parent);
		if(permissionSet!= null){
			makeChildren(parent, list,permissionSet);
		}else{
			makeChildren(parent, list);
		}
		return parent;
	}
	/**
	 * desc:
	 * <p>author: Jan.Zhang </p>
	 *  date : 2014-5-4 
	 * @param parent
	 * @param children
	 * @param permissionSet
	 */
	private void makeChildren(List<Module> parent, List<Module> children,Set<String> permissionSet) {
		if (children.isEmpty()) {
			return ;
		}
		List<Module> tmp = new ArrayList<Module>();
		for (Module c1 : parent) {
			for (Module c2 : children) {
				c2.setChildren(new ArrayList<Module>(0));
				if (c1.getId() == c2.getParent().getId()) {
					if(permissionSet.contains(c2.getServName() + ":" + SecurityConstants.OPERATION_VIEW)){
						c1.getChildren().add(c2);
					}
					tmp.add(c2);
				}
			}
		}
		children.removeAll(tmp);
		makeChildren(tmp, children,permissionSet);
	}
	
	private void makeChildren(List<Module> parent, List<Module> children) {
		if (children.isEmpty()) {
			return ;
		}
		List<Module> tmp = new ArrayList<Module>();
		for (Module c1 : parent) {
			for (Module c2 : children) {
				c2.setChildren(new ArrayList<Module>(0));
				if (c1.getId() == c2.getParent().getId()) {
					c1.getChildren().add(c2);
					tmp.add(c2);
				}
			}
		}
		children.removeAll(tmp);
		makeChildren(tmp, children);
	}
	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.impl.BaseServiceImpl#setMapper(com.umpay.sc.plat.core.mapper.BaseMapper)
	 */
	@Override
	public void setMapper(BaseDao<Module> mapper) {
		super.setMapper(mapper);
		this.mapper = (ModuleDao) mapper;
	}
}
